package com.peaw.access.controller.annotation;

import java.io.File;
import java.security.SecureRandom;
import java.util.List;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;

import org.apache.commons.io.FileUtils;

import com.thoughtworks.xstream.core.util.Base64Encoder;

public class Util {
	private static String CHARSET = "UTF-8";

	public static void main(String[] args) throws Exception {
		
		System.out.println(geneLicenceCode("2017-07-01"));
		
		System.out.println(anaLicenceCode());
		
	}
	
	public static String geneLicenceCode(String src) throws Exception {
		List<String> licContent = FileUtils.readLines(new File(
				Util.class.getResource("/ccb-demo.lic")
						.getFile()));
		String factor = licContent.get(1).trim();
		String pwd = genePasswd(factor);
		String miwen = encrypt(src, pwd);
		String pwdAndMiwen = pwd + miwen;
		Base64Encoder base64Encoder = new Base64Encoder();
		String licencecode = base64Encoder.encode((pwdAndMiwen).getBytes(CHARSET));
		return licencecode;
	}
	
	public static String anaLicenceCode() throws Exception {
		List<String> licContent = FileUtils.readLines(new File(
				Util.class.getResource("/ccb-demo.lic")
						.getFile()));
		String licencecode = licContent.get(3).trim().substring(4);
		Base64Encoder base64Encoder = new Base64Encoder();
		String pwdAndMiwen = new String(base64Encoder.decode(licencecode), CHARSET);
		String factor = licContent.get(1).trim();
		String pwd = genePasswd(factor);
		String miwen = pwdAndMiwen.substring(pwd.length(), pwdAndMiwen.length());
		return decrypt(miwen, pwd);
	}
	
	public static String genePasswd(String factor) throws Exception {
		Base64Encoder base64Encoder = new Base64Encoder();
		String p1 = base64Encoder.encode(factor.getBytes(CHARSET));
		String passwd = base64Encoder.encode(p1.getBytes(CHARSET));
		return passwd;
	}

	public static String encrypt(String data, String password) {
		try {
			SecureRandom random = new SecureRandom();
			DESKeySpec desKey = new DESKeySpec(password.getBytes(CHARSET));
			// 创建一个密匙工厂，然后用它把DESKeySpec转换成
			SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
			SecretKey securekey = keyFactory.generateSecret(desKey);
			// Cipher对象实际完成加密操作
			Cipher cipher = Cipher.getInstance("DES");
			// 用密匙初始化Cipher对象
			cipher.init(Cipher.ENCRYPT_MODE, securekey, random);
			// 现在，获取数据并加密
			// 正式执行加密操作
			Base64Encoder base64Encoder = new Base64Encoder();
			return base64Encoder.encode(cipher.doFinal(data.getBytes(CHARSET)));
		} catch (Throwable e) {
			e.printStackTrace();
		}
		return null;
	}

	public static String decrypt(String src, String password) throws Exception {
		// DES算法要求有一个可信任的随机数源
		SecureRandom random = new SecureRandom();
		// 创建一个DESKeySpec对象
		DESKeySpec desKey = new DESKeySpec(password.getBytes(CHARSET));
		// 创建一个密匙工厂
		SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
		// 将DESKeySpec对象转换成SecretKey对象
		SecretKey securekey = keyFactory.generateSecret(desKey);
		// Cipher对象实际完成解密操作
		Cipher cipher = Cipher.getInstance("DES");
		// 用密匙初始化Cipher对象
		cipher.init(Cipher.DECRYPT_MODE, securekey, random);
		// 真正开始解密操作
		Base64Encoder base64Encoder = new Base64Encoder();
		return new String((cipher.doFinal(base64Encoder.decode(src))));
	}

}
